<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2022-2099 李志刚（854378082@qq.com）
 * @Date: 2019-01-03 20:14:16
 * @Description: 所有非get请求的日志
 * @LastEditors: 李志刚
 * @LastEditTime: 2023-01-16 17:32:37
 * @FilePath: \www.gamemc.com\app\Http\Controllers\Console\Rbac\LogController.php
 */

namespace App\Http\Controllers\Console\Rbac;

use Carbon\Carbon;
use App\Models\Rbac\Log;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LogController extends Controller
{
    public function getList(Request $request)
    {
        try {
            $page = $request->input('page', 0);
            $size = $request->input('size', 10);
            $request_user = json_decode($request->request_user);
            // 超级管理员可以查看所有用户日志，其它人只能看自己的
            if ($request_user->id == '1') {
                $admin_id = $request->input('admin_id',0);
                $list = Log::where(function ($q) use ($admin_id) {
                    if ($admin_id != '0') {
                        $q->where('admin_id', $admin_id);
                    }
                })->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
                $total = Log::where(function ($q) use ($admin_id) {
                    if ($admin_id != '0') {
                        $q->where('admin_id', $admin_id);
                    }
                })->count();
            } else {
                $list = Log::where('admin_id', $request_user->id)->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
                $total = Log::where('admin_id', $request_user->id)->count();
            }
            $res = ['list' => $list, 'total' => $total];
            return $this->resJson(200, '成功', $res);;
        } catch (\Throwable $th) {
            return $this->resJson(500, '服务器异常，请稍后再试');
        }
    }
    public function postClear()
    {
        try {
            Log::where('created_at', '<', Carbon::now()->addMonth(-1))->delete();
            return $this->resJson(200, '成功');;
        } catch (\Throwable $th) {
            return $this->resJson(500, '服务器异常，请稍后再试');
        }
    }
}
